Script that replicates folder structure

Hi guys, I want to create a DXL script that recursively creates in DOORS a folder structure replicated after a Windows folder structure with an inputted Windows path, for ex: "C:\\test\\another_folder\\subfolder\\etc".

So far I only managed to replicate the folder content for only one folder, without being able to go deeper into the folder structure by using this:

int Folders()

string x = "C:\\test\\another_folder\\subfolder\\etc"
string file
for file in directory x do {
if ( ( file == "." or file == ".." ) )
{
deleteFile file
}
else
{
Folder f = create( file, "test")
}
print file "\n"
}

Can you help me out with some pointers on how I can get this to also search in the Windows subfolders of the current folder and replicate the folder structure in DOORS as well?
SystemAdmin - Mon Apr 01 08:58:51 EDT 2013

Re: Script that replicates folder structure
llandale - Tue Apr 02 10:31:27 EDT 2013

You do not want to "delete" the "." and ".." files.

Here is a recursive function I used to print the contents of a windows folder; I think it has the examples you need. The 2 lines that look like this:

  • if (NameFile == NameDir)

are suspicious and I would test them somehow.

-Louie

//*****************************************************************
void    SearchDir(string NameDir, bool Recurse, Buffer &bufResults)
{             // display the folders and files in the directory
                // Optionally Recurs through subfolders
                // This function is RECURSION enabled
        string  NameFile
        string  NameFull
        Stat    stat
 
        set (dbeStatus, "... '" NameDir "'")
        g_NumFolders++
        bufResults += "\n..." NameDir[g_OriginLen:]"\n"
 
                // *** [1] Print the info of all SubFolders
        for NameFile in directory NameDir do
        {  if (NameFile[0:0] == ".")        // skip the "." and ".." file names.  No other name may begin with '.'
              continue
           if (NameFile == NameDir)     // Don't recall why, but skip a file with exact name as Directory
           {  print  ">>SearchDir, NameFile == NameDir '" NameFile "'\n"
              continue
           }
           NameFull     = NameDir "\\" NameFile
           stat = create(NameFull)
//    print "considering >" NameFull "<\n"
           if (!null stat and directory(stat))
              DisplayFileInfo(stat, true, NameDir, NameFile, bufResults)
           if (!null stat) delete(stat)
        }
                // *** [2] Print the info of all Files
        for NameFile in directory NameDir do
        {  if (NameFile[0:0] == ".")
              continue
           if (NameFile == NameDir)
              continue
           g_NumFiles++
           NameFull     = NameDir "\\" NameFile
           stat = create(NameFull)
           if (!null stat and (regular(stat) or symbolic(stat)))
               DisplayFileInfo(stat, false, NameDir, NameFile, bufResults)
           if (!null stat) delete(stat)
        }
 
                // *** [3] Recurs through SubFolders
        if (Recurse)
        {  for NameFile in directory NameDir do
           {  if (NameFile[0:0] == ".")
                continue
              NameFull = NameDir "\\" NameFile
              stat      = create(NameFull)
              if (!null stat and directory(stat))
              {  // print "*** NOT Doing >" NameFull "<\n"
                 SearchDir(NameFull, Recurse, bufResults)       // ** RECURSION **
              }
              if (!null stat) delete(stat)
           }
        }
}    // end SearchDir()

Re: Script that replicates folder structure
Tony_Goodman - Wed Apr 03 05:44:52 EDT 2013

llandale - Tue Apr 02 10:31:27 EDT 2013

You do not want to "delete" the "." and ".." files.

Here is a recursive function I used to print the contents of a windows folder; I think it has the examples you need. The 2 lines that look like this:

  • if (NameFile == NameDir)

are suspicious and I would test them somehow.

-Louie

//*****************************************************************
void    SearchDir(string NameDir, bool Recurse, Buffer &bufResults)
{             // display the folders and files in the directory
                // Optionally Recurs through subfolders
                // This function is RECURSION enabled
        string  NameFile
        string  NameFull
        Stat    stat
 
        set (dbeStatus, "... '" NameDir "'")
        g_NumFolders++
        bufResults += "\n..." NameDir[g_OriginLen:]"\n"
 
                // *** [1] Print the info of all SubFolders
        for NameFile in directory NameDir do
        {  if (NameFile[0:0] == ".")        // skip the "." and ".." file names.  No other name may begin with '.'
              continue
           if (NameFile == NameDir)     // Don't recall why, but skip a file with exact name as Directory
           {  print  ">>SearchDir, NameFile == NameDir '" NameFile "'\n"
              continue
           }
           NameFull     = NameDir "\\" NameFile
           stat = create(NameFull)
//    print "considering >" NameFull "<\n"
           if (!null stat and directory(stat))
              DisplayFileInfo(stat, true, NameDir, NameFile, bufResults)
           if (!null stat) delete(stat)
        }
                // *** [2] Print the info of all Files
        for NameFile in directory NameDir do
        {  if (NameFile[0:0] == ".")
              continue
           if (NameFile == NameDir)
              continue
           g_NumFiles++
           NameFull     = NameDir "\\" NameFile
           stat = create(NameFull)
           if (!null stat and (regular(stat) or symbolic(stat)))
               DisplayFileInfo(stat, false, NameDir, NameFile, bufResults)
           if (!null stat) delete(stat)
        }
 
                // *** [3] Recurs through SubFolders
        if (Recurse)
        {  for NameFile in directory NameDir do
           {  if (NameFile[0:0] == ".")
                continue
              NameFull = NameDir "\\" NameFile
              stat      = create(NameFull)
              if (!null stat and directory(stat))
              {  // print "*** NOT Doing >" NameFull "<\n"
                 SearchDir(NameFull, Recurse, bufResults)       // ** RECURSION **
              }
              if (!null stat) delete(stat)
           }
        }
}    // end SearchDir()

Try this.

//  Create Doors Folder Structure from Windows Directory Structure
 
/*

    ...or create Doors from Windows!
        
   Tony Goodman

*/
 
pragma runLim, 0
 
DB   dbMain = null
DBE dbeRootDoorsFolder = null
DBE dbeRootWindowsDirectory = null
DBE dbeBrowseDoors = null
DBE dbeBrowseWindows = null
 
 
/*******************
        getWindowsFolder
********************/
string getWindowsFolder(string &dirPath)
{
        OleAutoObj  objDialog     = null
        OleAutoObj  objFolder     = null
        OleAutoObj  objFolderItem = null
        OleAutoArgs args          = create
        string      folderPath    = ""
        string      res           = ""
        
        dirPath = ""
        
        objDialog = oleCreateAutoObject("Shell.Application")
        
        if (null objDialog)
        {
                return("Failed to create dialog object")
        }
        
        put(args, 0)                         // always zero
        put(args, "Please select a folder")  // title
        put(args, 0)                         // options
 
        res = oleMethod(objDialog, "BrowseForFolder", args, objFolder)
 
 
        if (null objFolder)
        {
                if (res "" != "")
                {
                        return("Failed to launch browser: " res)
                }
                else
                {
                        return("")
                }
        }
 
        res = oleGet(objFolder, "Self", objFolderItem)
 
        if (null objFolderItem)
        {
                return("Failed to get folder item: " res)
        }
        
        oleGet(objFolderItem, "Path", folderPath)
 
        dirPath = folderPath
        
        return(res)
}
 
/*********************
        isDirectory

        Returns TRUE if string parameter is a valid windows directory.
*********************/
bool isDirectory(string dn)
{
        Stat s = create dn
 
        if (null s) return false
 
        if (directory s)
        {
                delete s
                return true
        }
        delete s
        return false
}
 
/**************************
        getShortName
**************************/
string getShortName(string dir)
{
        string shortName = ""
        int i = 0
        
        for (i = length(dir) - 1; i > 0; i--)
        {
                if (dir[i:i] == "\\")
                break
        }
        
        if (i > 0)
        {
                shortName = dir[i+1:]
        }
        
        return(shortName)
}
 
/**************************
        createFoldersFromDirectories
**************************/
void createFoldersFromDirectories(string doorsFolder, string windowsDirectory)
{
        string res = ""
    string f = ""
        string shortName = ""
        Folder newFolder = null
        
        
        (current FolderRef__) = folder(doorsFolder)
        
        shortName = getShortName(windowsDirectory)
        
        if (shortName == "")
        {
                infoBox("Invalid directory name: " windowsDirectory)
                return
        }
        
        // create DOORS folder
        res = create(shortName, "", newFolder)
        
        if (res "" != "")
        {
                infoBox(res)
                return
        }
        
        for f in directory windowsDirectory do
        {
                // ignore this directory and parent
                if (f == "." || f == "..") continue
 
                if (isDirectory(windowsDirectory "\\" f))
                {
                        createFoldersFromDirectories(fullName(newFolder), windowsDirectory "\\" f)
                }
    }
}
 
/**************************
        doBrowseWindows
**************************/
void doBrowseWindows(DBE dbe)
{
        string res = ""
        string dirName = ""
        
        res = getWindowsFolder(dirName)
        
        if (res == "")
        {
                set(dbeRootWindowsDirectory, dirName)
        }
        else
        {
                infoBox(res)
        }
}
 
/******************************************************************************
        doBrowseDoors
        
******************************************************************************/
void doBrowseDoors(DBE dbe)
{
    string szFolderName = fnMiniExplorer(dbMain, (current Folder), MINI_EXP_FP, "Select Folder", "Select Folder")
 
    // ensure that mini explorer has returned a valid module name
    if (szFolderName != "" && folder(szFolderName))
    {
        set(dbeRootDoorsFolder, szFolderName )
    }        
}
 
/**********************
        doOK
***********************/
void doOK(DB db)
{
        string rootWindows = ""
        string rootDoors = ""
        
        rootDoors = get(dbeRootDoorsFolder)
        rootWindows = get(dbeRootWindowsDirectory)
        
        if (!confirm("You are about to create a new folder structure.\nAre you sure you want to continue?"))
        {
                return
        }
        
        createFoldersFromDirectories(rootDoors, rootWindows)
        
        refreshDBExplorer()
}
 
 
/*********************
        MAIN
**********************/
dbMain = create("Create Folder Structure", styleCentered)
 
label(dbMain, "This utility will create a new folder structure in DOORS " //-
                    "that reflects the structure of the selected windows directory.\n")
dbeRootDoorsFolder = field(dbMain, "Current Folder:", fullName(current Folder), 60, false)
beside(dbMain)
dbeBrowseDoors = button(dbMain, "Browse...", doBrowseDoors)
left(dbMain)
dbeRootWindowsDirectory = field(dbMain, "Windows Directory:", "", 60, false)
beside(dbMain)
dbeBrowseWindows = button(dbMain, "Browse...", doBrowseWindows)
 
ok(dbMain, doOK)
 
realize dbMain
 
show(dbMain)

 


Tony Goodman, www.smartdxl.com

 

Re: Script that replicates folder structure
SystemAdmin - Wed Apr 03 09:47:07 EDT 2013

Tony_Goodman - Wed Apr 03 05:44:52 EDT 2013

Try this.

//  Create Doors Folder Structure from Windows Directory Structure
 
/*

    ...or create Doors from Windows!
        
   Tony Goodman

*/
 
pragma runLim, 0
 
DB   dbMain = null
DBE dbeRootDoorsFolder = null
DBE dbeRootWindowsDirectory = null
DBE dbeBrowseDoors = null
DBE dbeBrowseWindows = null
 
 
/*******************
        getWindowsFolder
********************/
string getWindowsFolder(string &dirPath)
{
        OleAutoObj  objDialog     = null
        OleAutoObj  objFolder     = null
        OleAutoObj  objFolderItem = null
        OleAutoArgs args          = create
        string      folderPath    = ""
        string      res           = ""
        
        dirPath = ""
        
        objDialog = oleCreateAutoObject("Shell.Application")
        
        if (null objDialog)
        {
                return("Failed to create dialog object")
        }
        
        put(args, 0)                         // always zero
        put(args, "Please select a folder")  // title
        put(args, 0)                         // options
 
        res = oleMethod(objDialog, "BrowseForFolder", args, objFolder)
 
 
        if (null objFolder)
        {
                if (res "" != "")
                {
                        return("Failed to launch browser: " res)
                }
                else
                {
                        return("")
                }
        }
 
        res = oleGet(objFolder, "Self", objFolderItem)
 
        if (null objFolderItem)
        {
                return("Failed to get folder item: " res)
        }
        
        oleGet(objFolderItem, "Path", folderPath)
 
        dirPath = folderPath
        
        return(res)
}
 
/*********************
        isDirectory

        Returns TRUE if string parameter is a valid windows directory.
*********************/
bool isDirectory(string dn)
{
        Stat s = create dn
 
        if (null s) return false
 
        if (directory s)
        {
                delete s
                return true
        }
        delete s
        return false
}
 
/**************************
        getShortName
**************************/
string getShortName(string dir)
{
        string shortName = ""
        int i = 0
        
        for (i = length(dir) - 1; i > 0; i--)
        {
                if (dir[i:i] == "\\")
                break
        }
        
        if (i > 0)
        {
                shortName = dir[i+1:]
        }
        
        return(shortName)
}
 
/**************************
        createFoldersFromDirectories
**************************/
void createFoldersFromDirectories(string doorsFolder, string windowsDirectory)
{
        string res = ""
    string f = ""
        string shortName = ""
        Folder newFolder = null
        
        
        (current FolderRef__) = folder(doorsFolder)
        
        shortName = getShortName(windowsDirectory)
        
        if (shortName == "")
        {
                infoBox("Invalid directory name: " windowsDirectory)
                return
        }
        
        // create DOORS folder
        res = create(shortName, "", newFolder)
        
        if (res "" != "")
        {
                infoBox(res)
                return
        }
        
        for f in directory windowsDirectory do
        {
                // ignore this directory and parent
                if (f == "." || f == "..") continue
 
                if (isDirectory(windowsDirectory "\\" f))
                {
                        createFoldersFromDirectories(fullName(newFolder), windowsDirectory "\\" f)
                }
    }
}
 
/**************************
        doBrowseWindows
**************************/
void doBrowseWindows(DBE dbe)
{
        string res = ""
        string dirName = ""
        
        res = getWindowsFolder(dirName)
        
        if (res == "")
        {
                set(dbeRootWindowsDirectory, dirName)
        }
        else
        {
                infoBox(res)
        }
}
 
/******************************************************************************
        doBrowseDoors
        
******************************************************************************/
void doBrowseDoors(DBE dbe)
{
    string szFolderName = fnMiniExplorer(dbMain, (current Folder), MINI_EXP_FP, "Select Folder", "Select Folder")
 
    // ensure that mini explorer has returned a valid module name
    if (szFolderName != "" && folder(szFolderName))
    {
        set(dbeRootDoorsFolder, szFolderName )
    }        
}
 
/**********************
        doOK
***********************/
void doOK(DB db)
{
        string rootWindows = ""
        string rootDoors = ""
        
        rootDoors = get(dbeRootDoorsFolder)
        rootWindows = get(dbeRootWindowsDirectory)
        
        if (!confirm("You are about to create a new folder structure.\nAre you sure you want to continue?"))
        {
                return
        }
        
        createFoldersFromDirectories(rootDoors, rootWindows)
        
        refreshDBExplorer()
}
 
 
/*********************
        MAIN
**********************/
dbMain = create("Create Folder Structure", styleCentered)
 
label(dbMain, "This utility will create a new folder structure in DOORS " //-
                    "that reflects the structure of the selected windows directory.\n")
dbeRootDoorsFolder = field(dbMain, "Current Folder:", fullName(current Folder), 60, false)
beside(dbMain)
dbeBrowseDoors = button(dbMain, "Browse...", doBrowseDoors)
left(dbMain)
dbeRootWindowsDirectory = field(dbMain, "Windows Directory:", "", 60, false)
beside(dbMain)
dbeBrowseWindows = button(dbMain, "Browse...", doBrowseWindows)
 
ok(dbMain, doOK)
 
realize dbMain
 
show(dbMain)

 


Tony Goodman, www.smartdxl.com

 

Guys, thank you so much, you wouldn't believe how much time this script saves me. You guys rock! :)